package com.shanzmoo.merchant.server.api.manager.merchant;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.shanzmoo.base.constant.e.LoginUserType;
import com.shanzmoo.db.module.merchant.domain.dto.MerchantOrderSaveDto;
import com.shanzmoo.db.module.merchant.domain.vo.MerchantOrderVo;
import com.shanzmoo.db.module.merchant.entity.MerchantOrderEntity;
import com.shanzmoo.db.module.merchant.service.IMerchantOrderService;
import com.shanzmoo.db.module.pay.entity.STerminalInfoEntity;
import com.shanzmoo.db.module.pay.service.ISTerminalInfoService;
import com.shanzmoo.merchant.server.api.domain.AuthUser;
import com.shanzmoo.merchant.server.api.util.SecurityUtil;
import com.shanzmoo.pay.domain.response.payResponse.SaobeiJsPayResponse;
import com.shanzmoo.pay.manager.SaobeiPayManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 订单管理manager
 * @author fs
 */
@Component
@RequiredArgsConstructor
public class MerchantOrderManager {
    private final IMerchantOrderService iMerchantOrderService;
    private final ISTerminalInfoService isTerminalInfoService;
    private final SaobeiPayManager saobeiPayManager;
    /**
     * 用户扫码下单
     * @param saveDto
     */
    public MerchantOrderVo createOrder(MerchantOrderSaveDto saveDto) {
        AuthUser authUser = SecurityUtil.getCurrentUser();
        MerchantOrderEntity data = saveDto.convert(MerchantOrderEntity.class);
        if(authUser.getUserType() == LoginUserType.SH_TEMP){
            data.setTmpMemberMerchantId(authUser.getUserId());
        }else if(authUser.getUserType() == LoginUserType.SH_HUIYUAN){
            data.setMemberMerchantId(authUser.getUserId());
        }else{
            Assert.isTrue(false,"当前用户类型不正确");
        }
        String openId;
        if(StrUtil.equals(saveDto.getPayType(),"010")){
            //微信
            openId = authUser.getWxOpenId();
            Assert.notBlank(openId,"该用户未绑定微信");
        }else if(StrUtil.equals(saveDto.getPayType(),"020")){
            //支付宝
            openId = authUser.getZfbOpenId();
            Assert.notBlank(openId,"该用户未绑定支付宝");
        }else{
            openId = null;
            Assert.isTrue(false,"当前类型无法扫码下单");
        }
        data.setStatus(0);
        STerminalInfoEntity terminalInfo = isTerminalInfoService
                .lambdaQuery()
                .eq(STerminalInfoEntity::getTerminalId,saveDto.getTerminalNo())
                .one();
        Assert.notNull(terminalInfo,"当前终端二维码存在问题");
        data.setMerchantCode(terminalInfo.getMerchantNo());
        data.setTerminalRecordNo(IdUtil.getSnowflake(1,2).nextIdStr());
        data.setPayStatus("5");
        SaobeiJsPayResponse response = saobeiPayManager.addPayOrder(data,openId,terminalInfo.getAccessToken());
        //TODO 调用支付接口生成支付单,在用户支付后回调修改订单payTime,payStatus,status字段
        data.setPayOrderCode(response.getOut_trade_no());
        Assert.isTrue(iMerchantOrderService.save(data),"订单创建失败");
        MerchantOrderVo vo = data.convert(MerchantOrderVo.class);
        vo.setPayResponse(response);
        return vo;
    }
}
